package nuc.ss.wl.controller;

import nuc.ss.wl.entity.Teacher;
import nuc.ss.wl.service.TeacherService;
import nuc.ss.wl.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

public class TeacherServlet extends BaseServlet {

    public void add(HttpServletRequest request,
                    HttpServletResponse response) throws ServletException, IOException {
        //从页面获取的信息(请求信息)
        //int数据处理
        int tno = Integer.parseInt(request.getParameter("tno"));
        String tname = request.getParameter("tname");

        //对象处理
        Teacher teacher = new Teacher();
        teacher.setTno(tno);
        teacher.setTname(tname);

        //调用业务层(完成添加)进行处理
        TeacherService teacherService = new TeacherServiceImpl();
        int i = teacherService.add(teacher);
        //根据结果进行页面跳转
        if(i>0){
            response.sendRedirect(request.getContextPath()+"/servlet/EmployeeServlet?method=findTea");
        }else{
            request.setAttribute("error","添加老师失败");
            request.getRequestDispatcher("/system/empAdd.jsp").forward(request, response);
        }

    }
    public void findTea(HttpServletRequest request,
                        HttpServletResponse response)throws ServletException,IOException {
        //接收查询条件
        int tno  = Integer.parseInt(request.getParameter("tno"));
        String tname = request.getParameter("tname");//

        //查询老师信息
        TeacherService teaService = new TeacherServiceImpl();
        List<Teacher> list = teaService.findTea(tno,tname);
        request.setAttribute("teaList", list);

        request.getRequestDispatcher("/system/empList.jsp").forward(request, response);
    }
    public void findAll(HttpServletRequest request,
                        HttpServletResponse response)throws ServletException,IOException {
        //查询老师信息
        TeacherService teacherService = new TeacherServiceImpl();
        List<Teacher> list = teacherService.findAll();

        request.setAttribute("list", list);

        //跳转到 system/empAdd.jsp中
        request.getRequestDispatcher("/system/empList.jsp").forward(request, response);
    }
    public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //接收视图层的表单数据
        int tno = Integer.parseInt(request.getParameter("tno"));
        //调用业务层完成添加方法
        TeacherService empService = new TeacherServiceImpl();
        empService.delete(tno);

        //跳转到??   不要直接跳转到页面，只是负责显示。要跳转到servlet--findAll，
        request.getRequestDispatcher("/servlet/DepartmentServlet?method=findAll").forward(request, response);

    }
    /**
     * 待更新数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void teacherFindById(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //接收页面信息
        int tno = Integer.parseInt(request.getParameter("tno"));
        //调用业务层方法
        TeacherService teacherService = new TeacherServiceImpl();
        Teacher teacher = teacherService.findById(tno);
        request.setAttribute("teacher", teacher);

        //页面跳转至 /system/empUpdate.jsp
        request.getRequestDispatcher("/system/empUpdate.jsp").forward(request, response);
    }
    /**
     * 更新数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //接收查询条件
        int tno  = Integer.parseInt(request.getParameter("tno"));
        String tname = request.getParameter("tname");//

        //调用业务层(完成添加)进行处理
        TeacherService teaService = new TeacherServiceImpl();
        Teacher teacher = new Teacher();
        teacher.setTno(tno);
        teacher.setTname(tname);
        int i = teaService.update(teacher);

        //根据结果进行页面跳转
        if(i>0){
            response.sendRedirect(request.getContextPath()+"/servlet/EmployeeServlet?method=findEmp");
        }else{
            request.setAttribute("error","更新教师失败");
            request.getRequestDispatcher("/system/empUpdate.jsp").forward(request, response);
        }
    }



}
